package com.itheima.reggie.common;

import com.itheima.reggie.contants.MessageConstant;
import com.itheima.reggie.ex.CustomerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * @author ghy
 * @version v1.0
 * create on 2022/11/17 10:48
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理CustomerException异常
     * @param ex 异常消息对象
     * @return
     */
    @ExceptionHandler(CustomerException.class)
    public R doCustomerException(CustomerException ex){
        log.error("doCustomerException:{}", ex);
        return R.error(ex.getMessage());
    }

    /**
     * 处理RuntimeException异常
     * @param ex 异常消息对象
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public R doRuntimeException(RuntimeException ex){
        log.error("doRuntimeException:{}", ex);
        return R.error(MessageConstant.GLOBAL_ERROR_MESSAGE);
    }

    /**
     * 处理Exception异常
     * @param ex 异常消息对象
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R doException(Exception ex){
        log.error("doException:{}", ex);
        return R.error(MessageConstant.GLOBAL_ERROR_MESSAGE);
    }


}
